package com.yq.controller;

import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.GOTO;
import com.yq.model.Result;
import com.yq.model.po.User;
import com.yq.model.vo.UserInfoVO;
import com.yq.service.AdminUserService;
import com.yq.service.impl.AdminUserServiceImpl;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.commons.lang3.StringUtils;

import javax.management.relation.RelationSupport;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/admin/user/*")
public class AdminUserServlet extends HttpServlet {
    AdminUserService adminUserService = new AdminUserServiceImpl();
    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/user/", "");
        if ("allUser".equals(op)){
            allUser(request,response);
        }else if ("deleteUser".equals(op)){
            deleteUser(request,response);
        }else if ("searchUser".equals(op)){
            searchUser(request,response);
        }

    }

    /**
     * 用户的查找，/api/admin/user/searchUser?word=a
     * @param request
     * @param response
     */

    private void searchUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取查找的条件
        String word = request.getParameter("word");
        List<UserInfoVO> list = adminUserService.searchUser(word);
        //TODO  {"code":0,"data":[{"id":1,"email":"admin","nickname":"admin","pwd":"admin","recipient":"admin","address":"admin","phone":"11111111111"}]}
        response.getWriter().println(gson.toJson(Result.ok(list)));


    }

    /**
     * 删除用户  /api/admin/user/deleteUser?id=2
     * @param request
     * @param response
     */
    private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)){
            response.getWriter().println(gson.toJson(Result.error("用户的id为空！")));
            return;
        }
        int userId = 0;
        try {
            userId = Integer.parseInt(id);

        }catch (Exception e){
            response.getWriter().println(gson.toJson(Result.error("id必须是数字")));
            return;
        }
        int code  = adminUserService.deleteById(userId);
        Result result=null;
        if (code==200){
            result=Result.ok();
        }else if (code==404){
            result=Result.error("删除失败");
        }
        response.getWriter().println(gson.toJson(result));

    }

    /**
     * 获取所有的用户信息
     * /api/admin/user/allUser
     * @param request
     * @param response
     */
    private void allUser(HttpServletRequest request, HttpServletResponse response) throws IOException {

        List<UserInfoVO> list = adminUserService.getAllUsersInfo();
        //返回的结果
        // TODO {"code":0,
        //  "data":
        //  [{"id":1,"email":"admin","nickname":"admin","pwd":"admin","recipient":"admin","address":"admin","phone":"11111111111"},
        //  {"id":6,"email":"12345@qq.com","nickname":"1111","pwd":"1","recipient":"在","address":"呃呃","phone":"13345678901"}]
        response.getWriter().println(gson.toJson(new Result(0,null,list)));
    }
}
